package com.gzhryc.shared_device.oem.h5.agent.mng.controllers.bxscn;

import com.gzhryc.common.data.dto.AjaxResult;
import com.gzhryc.common.exceptions.LogicException;
import com.gzhryc.servlet.DtoConstants;
import com.gzhryc.servlet.WebUtils;
import com.gzhryc.servlet.annotation.Route;
import com.gzhryc.shared_device.bxscn.code.BXSCNCostConfig;
import com.gzhryc.shared_device.bxscn.code.dao.db.BXSCNFeeTemplate;
import com.gzhryc.shared_device.bxscn.code.services.BXSCNFeeTemplateService;
import com.gzhryc.shared_device.cdb.code.CDBCostConfig;
import com.gzhryc.shared_device.cdb.code.dao.db.CDBFeeTemplate;
import com.gzhryc.shared_device.cdb.code.services.CDBFeeTemplateService;
import com.gzhryc.shared_device.oem.code.ConfigUtils;
import com.gzhryc.shared_device.oem.code.business.EBusinessType;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlaceBusinessConfig;
import com.gzhryc.shared_device.oem.code.place.dao.db.PlacePhysicalStore;
import com.gzhryc.shared_device.oem.code.place.services.PlaceBusinessConfigService;
import com.gzhryc.shared_device.oem.code.place.services.PlacePhysicalStoreService;
import com.gzhryc.shared_device.oem.code.user.dao.db.User;
import com.gzhryc.shared_device.oem.h5.agent.mng.WebConstants;

import javax.servlet.http.HttpServletRequest;

public class BXSCNDeviceCostController {

    @Route(value = { "/bxscn/place/cost.html" })
    public String placeCost(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request,"placeId");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
            if (place != null && place.getUserId() != null && place.getUserId().equals(loginUser.getId())) {
                request.setAttribute("userId", place.getUserId());

                BXSCNFeeTemplate feeTemplate = null;
                PlaceBusinessConfig businessConfig = PlaceBusinessConfigService.self().getByKeys(placeId, EBusinessType.BXSCN.index());
                if (businessConfig != null && businessConfig.getFeeTemplateId() != null) {
                    feeTemplate = BXSCNFeeTemplateService.self().getJoinById(businessConfig.getFeeTemplateId());
                }

                if (feeTemplate != null) {
                    WebUtils.setAttribute(request, feeTemplate);
                } else {
                    BXSCNCostConfig costConfig = ConfigUtils.getBXSCNCostConfig();
                    feeTemplate = BXSCNFeeTemplateService.buildByDefault(costConfig);
                    WebUtils.setAttribute(request, feeTemplate);
                }
            }
        }
        return "/bxscn/cost/place/config.html";
    }

    @Route(value = { "/bxscn/place/cost/change.do" })
    public AjaxResult doChange(HttpServletRequest request) {
        AjaxResult result = new AjaxResult(DtoConstants.EXECUTE_ERROR, "保存失败");
        Long placeId = WebUtils.getLong(request,"placeId");
        Long feeTemplateId = WebUtils.getLong(request,"feeTemplateId");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
            if (place != null && place.getUserId() != null && place.getUserId().equals(loginUser.getId())) {
                try {
                    if (PlaceBusinessConfigService.self().setFeeTemplateId(placeId, EBusinessType.BXSCN.index(), feeTemplateId)) {
                        result.setCode(DtoConstants.SUCCESS, null);
                    }
                } catch (LogicException e) {
                    result.setMsg(e.getMessage());
                }
            }
        }
        return result;
    }

    @Route(value = { "/bxscn/place/shop_cost.html" })
    public String placeShopCost(HttpServletRequest request) {
        Long placeId = WebUtils.getLong(request,"placeId");
        User loginUser = WebUtils.getAttribute(request, WebConstants.LOGIN_USER, User.class);
        if (loginUser != null) {
            PlacePhysicalStore place = PlacePhysicalStoreService.self().getById(placeId);
            if (place != null && place.getUserId() != null && place.getUserId().equals(loginUser.getId())) {
                request.setAttribute("userId", place.getUserId());
                CDBFeeTemplate feeTemplate = null;
                PlaceBusinessConfig deviceFunc = PlaceBusinessConfigService.self().getByKeys(placeId, EBusinessType.BXSCN_SHOP.index());
                if (deviceFunc != null && deviceFunc.getFeeTemplateId() != null) {
                    feeTemplate = CDBFeeTemplateService.self().getJoinById(deviceFunc.getFeeTemplateId());
                }


                if (feeTemplate != null) {
                    WebUtils.setAttribute(request, feeTemplate);
                } else {
                    CDBCostConfig costConfig = ConfigUtils.getCDBCostConfig();
                    feeTemplate = CDBFeeTemplateService.buildByDefault(costConfig);
                    WebUtils.setAttribute(request, feeTemplate);
                }
            }
        }
        return "/bxscn/cost/place/shop_config.html";
    }
}
